package ma.co.ba.BusinessApplications.presentation.controllers.listes;

import java.util.List;

import ma.co.ba.BusinessApplications.business.commons.CommonBusiness;
import ma.co.ba.BusinessApplications.business.profils.ProfilBusiness;
import ma.co.ba.BusinessApplications.business.utilisateurs.UtilisateurBusiness;
import ma.co.ba.BusinessApplications.model.Profil;
import ma.co.ba.BusinessApplications.model.Utilisateur;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/profils")
public class ProfilController {
	@Autowired
	UtilisateurBusiness utilisateurBusiness;
	@Autowired
	CommonBusiness commonBusiness;

	@Autowired
	ProfilBusiness profilBusiness;

	@RequestMapping(method = RequestMethod.GET)
	public String profilsPage(ModelMap model) {
		return "profils";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String processOnSubmit(
			@ModelAttribute("newProfil") Profil newProfil, ModelMap model) {

		commonBusiness.saveEntity(newProfil);

		return "redirect:/profils";
	}

	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String updateProfilPage(ModelMap model,
			@RequestParam("id") int profilId) {
		Profil upProfil = profilBusiness.getProfilById(profilId);
		model.addAttribute("upProfil", upProfil);
		return "editProfil";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateUtilisateurSubmit(ModelMap model,
			@ModelAttribute("upProfil") Profil upProfil) {

		commonBusiness.updateEntity(upProfil);
		return "redirect:/profils";
	}

	@ModelAttribute("newProfil")
	public Profil getNewProfil() {
		return new Profil();
	}

	@ModelAttribute("profilsList")
	public List<Profil> getProfilsList() {
		return profilBusiness.getAllProfils();
	}

	@ModelAttribute("utilisateurs")
	public List<Utilisateur> getUtilisateurs() {
		List<Utilisateur> utilisateurs = utilisateurBusiness
				.getAllConsultants();
		return utilisateurs;
	}
}
